package hash;

/**
 * Created with IntelliJ IDEA.
 * Description: 242.有效的字母异位词
 * User: Li_yizYa
 * Date: 2025—03—26
 * Time: 21:43
 */
public class Solution1 {
    public static void main(String[] args) {
        String str1 = "anagram";
        String str2 = "nagaram";
        System.out.println(isAnagram(str1, str2));
    }
    private static boolean isAnagram(String str1, String str2) {
        if (str1 == null || str2 == null || str1.length() != str2.length()) {
            return false;
        }
        boolean ret = true;
        int[] count = new int[26];
        for (int i = 0; i < str1.length(); i++) {
            count[str1.charAt(i) - 'a']++;
        }

        for (int i = 0; i < str2.length(); i++) {
            if (count[str2.charAt(i) - 'a'] == 0) {
                ret = false;
            } else {
                count[str2.charAt(i) - 'a']--;
            }
        }
        return ret;
    }

}
